﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Drawing;
using SlideIdeaDocument.Format.UIElement.Common;
using System.Windows;
using System.Windows.Media;

namespace BoolanElement
{
    public class BooTextBoxElement:BooElement
    {
        public double TextMarginTop { get; set; }
        public double TextMarginLeft { get; set; }
        public double TextMaxHeight { get; set; }
        public double TextWidth { get; set; }

        public bool IsFromTemplate = false;
        public event PropertyChangedEventHandler PropertyChanged;


        public double Width
        {
            get 
            {
                return base.ElementTransform.Width;
            }
            set
            {
                base.ElementTransform.Width = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Width"));
            }
        }
        public double Height
        {
            get
            {
                return base.ElementTransform.Height;
            }
            set
            {
                base.ElementTransform.Height = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Height"));
            }
        }

        private RichTextAlignment textAlignment;
        public RichTextAlignment TextAlignment 
        {
            get
            {
                return textAlignment;
            }
            set
            {
                textAlignment = value;
                if(PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("TextAlignment"));
            }
        }

        private ShapeStyle shapeStyle;
        public ShapeStyle ShapeStyle
        {
            get
            {
                return shapeStyle;
            }
            set
            {
                shapeStyle = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ShapeStyle"));
            }
        }

        private Color shapeFillColor;
        public Color ShapeFillColor
        {
            get
            {
                return shapeFillColor;
            }
            set
            {
                shapeFillColor = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ShapeFillColor"));
            }
        }

        public double ShapeWidth;
        public double ShapeHeight;

        private Color borderColor;
        public Color BorderColor 
        {
            get
            {
                return borderColor;
            }
            set
            {
                borderColor = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("BorderColor"));
            }
        }

        private double borderWidth;
        public double BorderWidth 
        {
            get
            {
                return borderWidth;
            }
            set
            {
                borderWidth = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("BorderWidth"));
            }
        }

        public string _Text;
        public string Text
        {
            set
            {
                _Text = value;
                UpdateText();
            }
            get
            {
                return _Text;
            }
        }

        public void UpdateText()
        {            
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Text"));
        }



        public XElement SaveShapeStyle()
        {
            XElement styleElement = new XElement("Style");

            XElement textMarginTop = new XElement("TextMarginTop");
            textMarginTop.Value = TextMarginTop.ToString();
            XElement textMarginLeft = new XElement("TextMarginLeft");
            textMarginLeft.Value = TextMarginLeft.ToString();
           // XElement textMaxHeight = new XElement("TextMaxHeight");
           // textMaxHeight.Value = TextMaxHeight.ToString();
            XElement textWidth = new XElement("TextWidth");
            textWidth.Value = TextWidth.ToString();
            XElement fontAlignment = new XElement("TextAlignment");
            fontAlignment.Value = TextAlignment.ToString();
            XElement shapeStyle = new XElement("ShapeStyle");
            shapeStyle.Value = ShapeStyle.ToString();
            XElement shapeFillColor = new XElement("ShapeFillColor");
            shapeFillColor.Value = SerializeCast.ColorToString(ShapeFillColor);
            XElement shapeWidth = new XElement("ShapeWidth");
            shapeWidth.Value = ShapeWidth.ToString();
            XElement shapeHeight = new XElement("ShapeHeight");
            shapeHeight.Value = ShapeHeight.ToString();
            XElement borderColor = new XElement("BorderColor");
            borderColor.Value = SerializeCast.ColorToString(BorderColor);
            XElement borderWidth = new XElement("BorderWidth");
            borderWidth.Value = BorderWidth.ToString();

            styleElement.Add(textMarginTop);
            styleElement.Add(textMarginLeft);
            //styleElement.Add(textMaxHeight);
            styleElement.Add(textWidth);
            styleElement.Add(shapeHeight);
            styleElement.Add(shapeWidth);
            styleElement.Add(fontAlignment);
            styleElement.Add(shapeStyle);
            styleElement.Add(borderColor);
            styleElement.Add(borderWidth);
            styleElement.Add(shapeFillColor);

            return styleElement;
        }

        public XElement SaveData()
        {
            XElement element = new XElement("Data");
            XElement stringData = new XElement("StringData");
            element.Add(stringData);

            stringData.Value = RichTextCoder.RtfToBase64(this.Text);

            return element;
        }

        public void LoadData(XElement element)
        {
            XElement ele = element.Element("StringData");
            Text = RichTextCoder.Base64ToRtf(ele.Value);
        }


        public void LoadShapeStyle(XElement element)
        {
            TextMarginTop = Double.Parse(element.Element("TextMarginTop").Value);
            TextMarginLeft = Double.Parse(element.Element("TextMarginLeft").Value);
            TextWidth = Double.Parse(element.Element("TextWidth").Value);
            TextAlignment = SerializeCast.StringToTextAlignment(element.Element("TextAlignment").Value);
            ShapeStyle = SerializeCast.StringToTextBoxStyle(element.Element("ShapeStyle").Value);
            BorderColor = SerializeCast.StringToColor(element.Element("BorderColor").Value);
            BorderWidth = Double.Parse(element.Element("BorderWidth").Value);
            ShapeFillColor = SerializeCast.StringToColor(element.Element("ShapeFillColor").Value);
            ShapeWidth = Double.Parse(element.Element("ShapeWidth").Value);
            ShapeHeight = Double.Parse(element.Element("ShapeHeight").Value);
        }


        public BooTextShapesBaseElement Clone(BooTextShapesBaseElement shapesElement)
        {
            return (BooTextShapesBaseElement)this.MemberwiseClone();
        }

        public static BooTextBoxElement InitElementStyle()
        {
            BooTextBoxElement textBoxElement = new BooTextBoxElement();
            textBoxElement.ElementTransform.Rotate.Angle = 0;
            textBoxElement.ElementTransform.Rotate.CenterX = 0;
            textBoxElement.ElementTransform.Rotate.CenterY = 0;

            textBoxElement.ElementTransform.Scale.ScaleX = 1;
            textBoxElement.ElementTransform.Scale.ScaleY = 1;
            textBoxElement.ElementTransform.Scale.CenterX = 0;
            textBoxElement.ElementTransform.Scale.CenterY = 0;

            textBoxElement.ElementTransform.Translate.X = 200;
            textBoxElement.ElementTransform.Translate.Y = 200;

            textBoxElement.ElementTransform.Margin = new Thickness(0, 0, 0, 0);

            textBoxElement.ElementTransform.Width = 400;
            textBoxElement.ElementTransform.Height = 200;

            textBoxElement.TextMarginLeft = 0;
            textBoxElement.TextMarginTop = 0;
            textBoxElement.TextWidth = 400;
         
            textBoxElement.TextAlignment = SerializeCast.StringToTextAlignment("LeftTop");

            textBoxElement.ShapeStyle = SerializeCast.StringToTextBoxStyle("BorderNoBKG");
            textBoxElement.BorderColor = System.Windows.Media.Color.FromArgb(255, 0, 0, 0);
            textBoxElement.BorderWidth = 0;

            textBoxElement.ShapeFillColor = Colors.Transparent;
            textBoxElement.ShapeWidth = 400;
            textBoxElement.ShapeHeight = 200;
           

            textBoxElement.Text = "";
            
            return textBoxElement;
        }

        public override XElement SaveToXElement()
        {
            XElement element = new XElement(typeof(BooTextBoxElement).Name);
            element.SetAttributeValue("UniqueID", UniqueID.ToString());
            element.Add(ElementTransform.Save());
            XElement styleElement = SaveShapeStyle();
            //save points
            element.Add(styleElement);

            element.Add(SaveData());

            return element;
        }

        public override BooElement Clone()
        {
            BooTextBoxElement element = new BooTextBoxElement();

            element.ElementTransform = this.ElementTransform.Clone();


            element.TextMarginLeft = this.TextMarginLeft;
            element.TextMarginTop = this.TextMarginTop;
            element.TextMaxHeight = this.TextMaxHeight;
            element.TextWidth = this.TextWidth;
          
            element.TextAlignment = this.TextAlignment;

            element.ShapeStyle = this.ShapeStyle;
            element.BorderColor = System.Windows.Media.Color.FromArgb(this.BorderColor.A, this.BorderColor.R, this.BorderColor.G, this.BorderColor.B);
            element.BorderWidth = this.BorderWidth;

            element.ShapeFillColor = System.Windows.Media.Color.FromArgb(this.ShapeFillColor.A, this.ShapeFillColor.R, this.ShapeFillColor.G, this.ShapeFillColor.B);
            element.ShapeWidth = this.ShapeWidth;
            element.ShapeHeight = this.ShapeHeight;

            element.Text = this.Text;

            return element;
        }

        public void ReStore(BooTextBoxElement element)
        {
            this.ElementTransform = element.ElementTransform.Clone();

            this.TextMarginLeft = element.TextMarginLeft;
            this.TextMarginTop = element.TextMarginTop;
            this.TextMaxHeight = element.TextMaxHeight;
            this.TextWidth = element.TextWidth;

            this.TextAlignment = element.TextAlignment;

            this.ShapeStyle = element.ShapeStyle;
            this.BorderColor = System.Windows.Media.Color.FromArgb(element.BorderColor.A, element.BorderColor.R, element.BorderColor.G, element.BorderColor.B);
            this.BorderWidth = element.BorderWidth;

            this.ShapeFillColor = System.Windows.Media.Color.FromArgb(element.ShapeFillColor.A, element.ShapeFillColor.R, element.ShapeFillColor.G, element.ShapeFillColor.B);
            this.ShapeWidth = element.ShapeWidth;
            this.ShapeHeight = element.ShapeHeight;

            this.Text = element.Text;

        }

        public override void LoadFromXElement(XElement element)
        {
            XElement tranformElement = element.Element("Transform");
            XElement dataElement = element.Element("Data");
            XElement styleElement = element.Element("Style");

            ElementTransform.Load(tranformElement);
            LoadShapeStyle(styleElement);
            Points = BooShapesPoints.LoadPoints(styleElement.Element("Points").Elements());
            LoadData(dataElement);
        }
    }
}
